﻿/*
 * Copyright © 2020, Wuxi Safover Intelligent Technology Co., Ltd. All rights reserved.
 * 
 * 无锡安永智能科技有限公司版权所有，未经许可，不得用于商业用途
 * 
 * Visit: https://www.safover.com/ 
 */

using log4net;
using Newtonsoft.Json;
using System;
using System.Reflection;

namespace Safover.LidarCollector.Extensions
{
    public sealed class BaseConfig<T> where T : new()
    {
        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public static T FromJson(string file, JsonSerializerSettings settings = null)
        {
            try
            {
                return Json.DeserializeObject<T>(file, settings);
            }
            catch (Exception e)
            {
                Log.Error(e.Message, e);
                return new T();
            }
        }

        public static void SaveJson(T config, string file, JsonSerializerSettings settings = null)
        {
            Json.SerializeObject(config, file, settings);
        }
    }
}
